libobs_simple\sources\windows\sources/
window_capture.rs1use crate::define_object_manager;
2
3use super::{ObsWindowCaptureMethod, ObsWindowPriority};
4#[cfg(feature = "window-list")]
5use libobs_window_helper::{get_all_windows, WindowInfo, WindowSearchMode};
6use libobs_wrapper::{
7 data::{ObsObjectBuilder, ObsObjectUpdater},
8 scenes::ObsSceneRef,
9 sources::{ObsSourceBuilder, ObsSourceRef},
10 utils::ObsError,
11};
12use num_traits::ToPrimitive;
13
14define_object_manager!(
15 #[derive(Debug)]
17 struct WindowCaptureSource("window_capture") for ObsSourceRef {
18
19 #[obs_property(type_t = "enum")]
30 priority: ObsWindowPriority,
31
32 #[obs_property(type_t = "string", settings_key = "window")]
42 window_raw: String,
43
44 #[obs_property(type_t = "bool")]
45 cursor: bool,
47
48 #[obs_property(type_t = "bool")]
49 capture_audio: bool,
53
54 #[obs_property(type_t = "bool")]
55 force_sdr: bool,
57
58 #[obs_property(type_t = "bool")]
59 client_area: bool,
61
62 #[obs_property(type_t = "bool")]
63 compatibility: bool,
64
65 capture_method: Option<ObsWindowCaptureMethod>,
66});
67
68#[cfg(feature = "window-list")]
69#[libobs_simple_macro::obs_object_impl]
70impl WindowCaptureSource {
71 pub fn get_windows(
73 mode: WindowSearchMode,
74 ) -> anyhow::Result<Vec<libobs_wrapper::unsafe_send::Sendable<WindowInfo>>> {
75 Ok(get_all_windows(mode)?
76 .into_iter()
77 .map(libobs_wrapper::unsafe_send::Sendable)
78 .collect())
79 }
80
81 pub fn set_window(self, window: &libobs_wrapper::unsafe_send::Sendable<WindowInfo>) -> Self {
91 self.set_window_raw(window.0.obs_id.as_str())
92 }
93}
94
95impl<'a> WindowCaptureSourceUpdater<'a> {
96 pub fn set_capture_method(mut self, method: ObsWindowCaptureMethod) -> Self {
97 self.get_settings_updater()
98 .set_int_ref("method", method.to_i32().unwrap() as i64);
99
100 self
101 }
102}
103
104impl WindowCaptureSourceBuilder {
105 pub fn set_capture_method(mut self, method: ObsWindowCaptureMethod) -> Self {
107 self.capture_method = Some(method);
108 self
109 }
110}
111
112impl ObsSourceBuilder for WindowCaptureSourceBuilder {
113 fn add_to_scene(mut self, scene: &mut ObsSceneRef) -> Result<ObsSourceRef, ObsError>
114 where
115 Self: Sized,
116 {
117 self.get_settings_updater().set_int_ref(
119 "method",
120 ObsWindowCaptureMethod::MethodAuto.to_i32().unwrap() as i64,
121 );
122
123 let method_to_set = self.capture_method;
124 let runtime = self.runtime.clone();
125
126 let b = self.build()?;
127 let mut res = scene.add_source(b)?;
128
129 if let Some(method) = method_to_set {
130 WindowCaptureSourceUpdater::create_update(runtime, &mut res)?
131 .set_capture_method(method)
132 .update()?;
133 }
134
135 Ok(res)
136 }
137}